//
//  CMCCIOTIGUnity.m
//  ig
//
//  Created by s on 16/6/15.
//  Copyright © 2016年 CMCCIOT. All rights reserved.
//

#import "CMCCIOTIGUnity.h"


@implementation CMCCIOTIGUnity

+(UIImage *)getImageByUIView:(CGRect  )imageRect  and:(UIColor  *)color{
    
    UIGraphicsBeginImageContext(imageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, imageRect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}
+(BOOL)regexTwoDecimalPlaces:(NSString  *)strTitle{

    BOOL   isTwoDecimal=NO;
    
    NSArray  * arrUpTxt=[strTitle  componentsSeparatedByString:@"."];
    //   如果为小数，判断是否是两位小数
    if (arrUpTxt.count) {
        if (arrUpTxt.count==2) {
           NSString  *strTemUp= arrUpTxt[1];
          isTwoDecimal=strTemUp.length==2?YES:NO;
        }
        else{
            isTwoDecimal=NO;
        }
    }
    
    return   isTwoDecimal;

}

+(NSDictionary  *)getCurrentDicAddress:(NET_WORKD_TYPE )netWsorkType{
   
    return   [self  getAllAddress][netWsorkType];
    
}


+(id)getUserDefault:(NSString  *)key{

    id  userInfo=[[NSUserDefaults standardUserDefaults] objectForKey:key];
   return  userInfo;

}

+(void)setUserDefault:(id)object andKey:(NSString *)key{
    [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    [[NSUserDefaults standardUserDefaults]  synchronize];
}

+(void)removeUserDefaultObjectForKey:(NSString  *)key{

    [[NSUserDefaults  standardUserDefaults] removeObjectForKey:key];
}

+(UIView  *)getViewByNibNameForIndex:(NSInteger)index andFrame:(CGRect )recFrame{
    
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CMCCIOTIGUnityView" owner:self options:nil];
    UIView *tmpCustomView = [nib objectAtIndex:index];
    [tmpCustomView  setFrame:recFrame];
    
    return tmpCustomView;
}

+(void)setWifiInfoCountTime{
    
    //  倒计时10秒
    
    NSString  *strMessage=@"设置成功，需要重启WiFi才能生效，请%d秒后进行操作";
    
    NSInteger  timeLine=10;
    CMCCIOTIGAlertControlCustomer  *alertControlCustomer=  [CMCCIOTTipsCenter  showAlertTitle:@"" message:[NSString stringWithFormat:strMessage,timeLine] cancelTitle:@"" cancelBlock:nil quireTitle:@"" quireBlock:nil];
    [CMCCIOTIGSharePopWindow  sharedWindow].closeType=CMCC_CLOSE_ONLY_BUTTON;
    
    
    //倒计时时间
    __block NSInteger timeOut = timeLine;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        //倒计时结束，关闭
        if (timeOut < 0) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [CMCCIOTIGSharePopWindow  sharedWindow].closeType=CMCC_CLOSEDefault;
                
                [[CMCCIOTIGSharePopWindow  sharedWindow]  dismiss];
                
                
            });
        } else {
            
            
            NSInteger allTime = (NSInteger)timeLine+1;
            NSInteger seconds = timeOut % allTime;
            NSString *timeStr = [NSString stringWithFormat:strMessage, seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                alertControlCustomer.lableMessage.text=timeStr;
                
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
    
}

+(NSDictionary    *)getRequestJson:(id  )dicJson{

    
    NSData *dataJson=[NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
    NSString *strJson=[[NSString alloc] initWithData:dataJson encoding:NSUTF8StringEncoding];
    
    NSDictionary *desStr=@{@"Jsons": strJson};
    return  desStr;
}

+(CGSize )getSize:(NSString  *)strText andSize:(CGSize)size  and:(UIFont *)fontSize{

    NSDictionary *attributes = @{NSFontAttributeName : fontSize};
    CGSize  lableSize = [strText boundingRectWithSize:CGSizeMake(size.width,size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    return  lableSize;

}

+(BOOL)isIllegal:(NSString *)strParama{
   
    NSArray  *arrIllegal=@[@"count", @"chr", @"mid", @"master",@"truncate", @"char", @"join",@"insert", @"select", @"delete", @"update",@"create",@"drop",
                           @"<",@">",@"/*",@"*/",@"’",@"|",@";",@"$",@"\"",@"\\",@"()",@"+",@",",@"0x0d",@"0x0a",@"OX0D",@"0X0A",@"０ｘ０ｄ",@"０Ｘ０Ｄ",@"０ｘ０ａ",@"０Ｘ０Ａ"];
    
    BOOL  isHas=NO;
    for (int i=0; i<arrIllegal.count; i++) {
        NSString *str=arrIllegal[i];
        if ([str isEqualToString:strParama]) {
            isHas=YES;
            break;
        }
        
    }
    
    return  isHas;

}

+(NSString *)getDateOfyyyyMMdd:(NSDate *)inputDate{
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [outputFormatter stringFromDate:inputDate];
    return str;
    
}
+(NSString  *)getJons:(id)dicJson{

    NSData *dataJson=[NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
    NSString *strJson=[[NSString alloc] initWithData:dataJson encoding:NSUTF8StringEncoding];
    return strJson;
    

}



@end
